<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\BadModel;
use think\Request;
use think\Session;
use think\db\Query;


class Buser extends Controller
{
    // 商业用户首页
    public function index()
    {
        $username = Session::get('username');
        $id = Session::get('id');
//        halt($id);
        if (null == $id) {
            return $this->error('请先登录', url('index/login/index'));
        }

        $list = Db::field(['b.username', 'b.id', 'p.name', 'p.uid', 'a.uid', 'a.title', 'a.path', 'i.photo'])
            ->table(['wb_buser' => 'b', 'wb_photofolder' => 'p', 'wb_bad' => 'a', 'wb_buserinfo' => 'i'])
            ->where('b.id = p.uid and b.id = a.uid and b.id= ' . $id)
            ->where('b.role=2')
            ->select();
//        halt($list);


        $res = Db::table(['wb_bad' => 'a', 'wb_buser' => 'b'])
            ->field(['a.path', 'b.id','a.link'])
            ->where('b.id=a.uid and b.id= ' . $id)
            ->where('b.role=2')
            ->select();


        $pname = Db::field(['p.name'])
            ->table(['wb_buser' => 'b', 'wb_photofolder' => 'p'])
            ->where('b.id = p.uid and b.id= ' . $id)
            ->where('b.role=2')
            ->select();
//      halt($pname);

        $message = Db::table('wb_message m')
            ->join('wb_buser b', 'm.uid=b.id', 'left')
            ->join('wb_mpicture p', 'p.mid=m.id ', 'left')
            ->join('wb_buserinfo i','b.id=i.uid','left')
            ->field(['m.content', 'm.time', 'm.collectnum', 'm.commentnum', 'm.transpondnum', 'm.agreenum', 'm.readnum', 'b.username', 'm.id', 'group_concat(p.path) path','i.photo','m.src_id','b.id uid'])

            ->where('b.id=' . $id)
            ->where('m.uid=' . $id)
            ->order('m.id desc')
            ->group('m.id')
            ->where('b.role=2')
            ->select();
//        halt($message);
        foreach ($message as $k => $v) {

            if (!empty($v['src_id'])){
//                halt($v['src_id']);
                $res1 = Db::field('m.id id,u.username username,photo,content,time,m.id uid')
                    ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])
                    ->find();
//                halt($res1);
                $message[$k]['old'] = $res1;
                $path = Db::field('path')
                    ->table(['wb_message' => 'm','wb_mpicture'=>'mp'])
                    ->where('m.id = '.$v['src_id'].' and mp.mid = m.id')
                    ->find();
                if (!empty($path)){
                    $message[$k]['old']['path'] = $path;
                }
            }

        }
//        halt($message);


        $articles = Db::table(['wb_buser' => 'b', 'wb_articles' => 'a'])
            ->field(['b.id', 'b.username', 'a.title', 'a.description', 'a.thumbnail', 'a.content', 'a.uid', 'a.status', 'b.role'])
            ->where('b.id = a.uid  and b.id=' . $id)
            ->where('b.role=2')
            ->select();
//        halt($articles);
        $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
            ->field(['i.photo', 'b.username'])
            ->where('b.id=i.uid and b.id=' . $id)
            ->where('b.role=2')
            ->find();
        $attent = Db::table('wb_attent a')
            ->join('wb_buser b', 'a.attent_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username','a.attent_id','b.role'])
            ->where('a.user_id=' . $id)
            ->where('b.role=2')
            ->select();
//            halt($attent);
        $fans = Db::table('wb_fans f')
            ->join('wb_buser b', 'f.fans_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username','f.fans_id','b.role'])
            ->where('f.user_id=' . $id)
            ->where('b.role=2')
            ->select();
        $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $id)
            ->find();

        $role = implode("", $arr);

        $p = Db::field('i.photo')
            ->table(['wb_buserinfo i','wb_buser b'])
            ->where('i.uid = '.$id)
            ->where('b.id=i.uid')
            ->where('b.role=2')
            ->find();
            $photo = implode("",$p);

        $picture = Db::field('p.name')
            ->table(['wb_photofolder p','wb_buser b'])
            ->where('p.uid = '.$id)
            ->where('b.id=p.uid')
            ->where('b.role=2')
            ->select();
//        halt($photo);

        $style = Db::field('style,id')
            ->table(['wb_style'])
            ->order(['id' => 'asc'])
            ->select();
//        halt($res);
        $id_p = Session::get('id');
        $pto = Db::table('wb_buserinfo')->field('photo')->where('uid',$id_p)->find();
        return view('index@buser/index', [
            'list' => $list,
            'res' => $res,
            'pname' => $pname,
            'username' => $username,
            'message' => $message,
            'photo'=>$photo,
            'articles' => $articles,
            'info' => $info,
            'attent' => $attent,
            'fans' => $fans,
            'picture'=>$picture,
            'style'=>$style,
            'id'=>$id,
            'pto'=>$pto
//            'attent_id'=>$attent_id
        ]);


    }

    // 相册
    public function photo()
    {
        $username = Session::get('username');
        $id = Session::get('id');
        $res = Db::field(['b.username', 'b.id', 'p.name', 'p.uid'])
            ->table(['wb_buser' => 'b', 'wb_photofolder' => 'p'])
            ->where(' b.id = p.uid and b.id= ' . $id)
            ->select();
//        halt($res);
        $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $id)
            ->find();

        $role = implode("", $arr);

        $p = Db::field('i.photo')
            ->table(['wb_buserinfo i','wb_buser b'])
            ->where('i.uid = '.$id)
            ->where('b.id=i.uid')
            ->where('b.role=2')
            ->find();
        $photo = implode("",$p);

        return view('index@buser/photo', [
            'res' => $res,
            'username' => $username,
            'photo'=>$photo,
            'id'=>$id
        ]);
    }

    // 服务
    public function server()
    {
        $username = Session::get('username');
        $id = Session::get('id');


        $list = Db::field(['b.username', 'b.id', 'a.title', 'a.description', 'a.keywords', 'a.thumbnail', 'a.content', 'a.add_time', 'a.uid', 'a.status'])
            ->table(['wb_buser' => 'b', 'wb_articles' => 'a'])
            ->where(' b.id = a.uid and b.id= ' . $id)->select();
      $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $id)
            ->find();

        $role = implode("", $arr);

        $p = Db::field('i.photo')
            ->table(['wb_buserinfo i','wb_buser b'])
            ->where('i.uid = '.$id)
            ->where('b.id=i.uid')
            ->where('b.role=2')
            ->find();
            $photo = implode("",$p);
        return view('index@buser/server', [
            'list' => $list,
            'username' => $username,
            'photo'=>$photo,
            'id'=>$id

        ]);
    }

    // 广告添加
    public function bad()
    {
        $username = Session::get('username');
        $id = Session::get('id');

//        halt($username);
        $list = Db::field(['b.username', 'b.id', 'a.uid'])
            ->table(['wb_buser' => 'b', 'wb_bad' => 'a'])
            ->where(' b.id = a.uid and b.id= ' . $id)->find();
//        halt($list);
        $p = Db::field('i.photo')
            ->table(['wb_buserinfo i','wb_buser b'])
            ->where('i.uid = '.$id)
            ->where('b.id=i.uid')
            ->where('b.role=2')
            ->find();
        $photo = implode("",$p);
        return view('index@buser/bad', [
            'list' => $list,
            'username' => $username,
            'photo'=>$photo,
            'id'=>$id
        ]);

    }

    // 执行添加广告
    public function save(Request $request)
    {
       // halt(111);
        $username = Session::get('username');
        $id = Session::get('id');

        $bad = new BadModel($request->post());
        $file = request()->file('path');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                // 输出 jpg
                $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                $info->getFilename();
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }

        }
        $res = '/uploads/' . $info->getSaveName();
        $res = str_replace("\\", "/", $res);
        // halt($res);
        // halt(request()->post());
        $bad->path = $res;
        $bad->uid = $id;

        $check = $this->validate($request->post(), 'index/BadValidate');

        if ($check !== true) {
            $this->error($check);
            return;
        }
        // 添加数据,并过滤非数据表字段
        // 返回 写入的记录数
        // halt($check);
        $result = $bad->allowField(true)->save();
       // var_dump($result);

        // 获取自订ID
        // echo $bad->id;

        // 判断指定情况
        if ($result > 0) {
            return $this->success('添加成功', url('/buser'));
        } else {
            return $this->error('添加失败,请重试!');
        }
    }

    // 删除微博
    public function delWeibo()
    {
//        $username = Session::get('username');
        $id = $_GET['id'];
        //       halt($id);
        $res = Db::table('wb_message')
            ->where('id=' . $id)
            ->delete();
//        halt($res);
        return join([
            'res' => $res,
            'id' => $id
        ]);
    }



}
